/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.legendshop.filter.LoginFilter;
import com.legendshop.oa.exception.MappingExceptionResolver;
import com.legendshop.oa.filter.LegendFilter;
import com.legendshop.oa.listener.InitSysListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesView;
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;

/**
 * 启动类.
 */
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}, scanBasePackages = "com.legendshop")
@ImportResource({"classpath:spring/applicationContext.xml"})

@PropertySource({
        "classpath:config/common.properties",
        "classpath:config/db.properties",
        "classpath:config/global.properties",
        "classpath:config/jdbc.properties"
    }) // 引用其他单独的properties

public class OaWebApplication {

	/** 日志. */
	private final static Logger log = LoggerFactory.getLogger(OaWebApplication.class);
	/**
	 * 主方法.
	 *
	 * @param args the args
	 */
	public static void main(String[] args) {
		SpringApplication.run(OaWebApplication.class, args);
	}
	
	/**
	 * 初始化Listener
	 * @return
	 */
    @Bean
    public ServletListenerRegistrationBean<InitSysListener> servletListenerRegistrationBean(){
        ServletListenerRegistrationBean<InitSysListener> servletListenerRegistrationBean = new ServletListenerRegistrationBean<InitSysListener>();
        servletListenerRegistrationBean.setListener(new InitSysListener());
        return servletListenerRegistrationBean;
    }
    
    /**
     * filter
     * @return
     */
    @Bean
    public FilterRegistrationBean<LoginFilter> indexFilterRegistration() {
        FilterRegistrationBean<LoginFilter> registration = new FilterRegistrationBean<LoginFilter>(new LoginFilter());
        registration.addUrlPatterns("/*");
        return registration;
    }


    @Bean
    public FilterRegistrationBean<LegendFilter> indexLegendFilter() {
        FilterRegistrationBean<LegendFilter> registration = new FilterRegistrationBean<LegendFilter>(new LegendFilter());
        registration.addUrlPatterns("/*");
        return registration;
    }


    /**
     * 初始化tiles
     * @return
     */
    @Bean
    public TilesConfigurer tilesConfigurer() {
        final TilesConfigurer configurer = new TilesConfigurer();
        configurer.setDefinitions(new String[] { "classpath*:/tiles/**/*definitions.xml" });
        configurer.setCheckRefresh(true);
        return configurer;
    }

    @Bean
    public TilesViewResolver tilesViewResolver() {
        final TilesViewResolver resolver = new TilesViewResolver();
        resolver.setViewClass(TilesView.class);
        resolver.setOrder(0);
        return resolver;
    }
    
    @Bean(name="exceptionResolver")
    public HandlerExceptionResolver parseMappingExceptionResolver(){
    	MappingExceptionResolver resolver = new MappingExceptionResolver();
    	resolver.setDefaultErrorView("/pages/common/errorFrame");
    	resolver.setDefaultAdminErrorView("/pages/common/errorAdminFrame");
    	resolver.setWarnLogCategory("WARN");
    	resolver.setDefaultStatusCode(500);
    	
    	return resolver;
    }
    
    /**
     * fastjson支持
     * @return
     */
    @Bean
    public HttpMessageConverters fastjsonHttpMessageConverter(){
        //定义一个转换消息的对象
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        //添加fastjson的配置信息 比如 ：是否要格式化返回的json数据
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //在转换器中添加配置信息
        fastConverter.setFastJsonConfig(fastJsonConfig);
        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }
    

}
